<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="knowledgeSort" class="menu" node-key="sortId" :data="knowledgeSortList" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false"
							 :filter-node-method="groupFilterNode" @node-click="groupClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
								<span class="do">
									<el-button-group>
										<el-button icon="el-icon-edit" size="small" @click.stop="knowledgeSortEdit(data)"></el-button>
										<el-button icon="el-icon-delete" size="small" @click.stop="knowledgeSortDel(node, data)"></el-button>
									</el-button-group>
								</span>
							</span>
						</template>

					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" style="width: 100%;" @click="addKnowledgeSort">添加分类</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="addKnowledge" :disabled="sortId==''">创建</el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<el-form-item label="创建人" style="padding-top: 20px;">
						<select-user-dialog v-model:accountId="search.accountId" :mode="false"/>
					</el-form-item>
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="标题 / 摘要" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" :params="this.search" row-key="knowledgeId" @selection-change="selectionChange" stripe remoteSort>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="知识标题" prop="title" width="300" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="知识摘要" prop="subheading" width="300" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="知识分类" prop="sortName" width="100"></el-table-column>
					<el-table-column label="版本" prop="version" width="100"></el-table-column>
					<el-table-column label="星级" prop="levelStar" width="50">
						<template #default="scope">
							{{scope.row.levelStar}}/星
						</template>
					</el-table-column>
					<el-table-column label="相关附件" prop="attachId" width="200" :show-overflow-tooltip="true">
						<template #default="scope">
							<attach-menu :attachIds="scope.row.attachId" :roleFlag="4" :preview="true"></attach-menu>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="240">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
							<el-dropdown>
								<el-button size="small">更多...</el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="resetKnowledgeIndex(scope.row, scope.$index)">重建索引</el-dropdown-item>
										<el-dropdown-item @click="deleteKnowledgeIndex(scope.row, scope.$index)">删除索引</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<knowledgeSortDialog v-if="dialog.knowledgeSort" ref="knowledgeSortDialog" @success="handleKnowledgeSortSuccess" @closed="dialog.knowledgeSort=false"></knowledgeSortDialog>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<detailsKnowledge v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></detailsKnowledge>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import knowledgeSortDialog from "./knowledgeSort";
import detailsKnowledge from './detailsKnowledge'
import saveDialog from "./save";
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	name: 'Knowledge Init Manage',
	components: {
		scSelectFilter,
		knowledgeSortDialog,
		saveDialog,
		selectUserDialog,
		detailsKnowledge,
		AttachMenu
	},
	data() {
		return {
			sortId:'',
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				}],
			apiObj: this.$API.file.knowledge.getMyCreateKnowledgeList,
			selection: [],
			dialog: {
				knowledgeSort: false,
				save: false,
				detailsSave:false
			},
			showGrouploading: false,
			groupFilterText: '',
			fileList:[],
			knowledgeSortList: [],
			groupsProps: {
				label: "name",
				value: "sortId"
			},
			search: {
				date:'',
				sortId:"",
				accountId:"",
				keyword: ""
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.knowledgeSort.filter(val);
		}
	},
	mounted() {
		this.getKnowledgeSortTree()
	},
	methods: {
		async resetKnowledgeIndex(row,index){
			var res = await this.$API.file.knowledge.resetKnowledgeIndex.post({knowledgeId:row.knowledgeId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async deleteKnowledgeIndex(row,index)
		{
			var res = await this.$API.file.knowledge.deleteKnowledgeIndex.post({knowledgeId:row.knowledgeId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//加载树数据
		async getKnowledgeSortTree() {
			this.showGrouploading = true;
			var res = await this.$API.file.knowledge.getKnowledgeSortTree.get();
			this.showGrouploading = false;
			this.knowledgeSortList = res.data;
		},
		filterChange(data){
			this.search.date = data.date;
			this.search.sortId = data.sortId;
			this.$refs.table.upData(this.search)
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.sortId = data.sortId
			this.search.sortId = data.sortId
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			this.$refs.table.refresh();

		},
		async handleKnowledgeSortSuccess() {
			var res = await this.$API.file.knowledge.getKnowledgeSortTree.get();
			if (res.code == 200) {
				this.knowledgeSortList = res.data;
			}
		},
		addKnowledgeSort() {
			this.dialog.knowledgeSort = true
			this.$nextTick(() => {
				this.$refs.knowledgeSortDialog.open()
			})
		},
		knowledgeSortEdit(data) {
			this.dialog.knowledgeSort = true
			this.$nextTick(() => {
				var editNode = this.$refs.knowledgeSort.getNode(data.sortId);
				var editNodeParentId = editNode.level == 1 ? '0' : editNode.parent.data.sortId;
				data.parentId = editNodeParentId
				this.$refs.knowledgeSortDialog.open("edit").setData(data)
			})

		},
		knowledgeSortDel(node, data) {
			this.$confirm(`确定删除 ${data.name} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.file.knowledge.deleteKnowledgeSort.post({sortId: data.sortId});
				if (res.code == 200) {
					this.$refs.knowledgeSort.remove(data.sortId)
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}

			}).catch(() => {

			})
		},
		addKnowledge() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add").setData({sortId:this.sortId});
			})
		},
		table_edit(row,index){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row)
			})
		},
		batch_del() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var knowledgeIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					knowledgeIdArr.push(this.selection[i].knowledgeId);
				}
				var res = await this.$API.file.knowledge.deleteKnowledgeByIds.post({ids:knowledgeIdArr});
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		async table_del(row,index){
			const loading = this.$loading();
			var res = await this.$API.file.knowledge.deleteKnowledge.post({knowledgeId:row.knowledgeId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		table_show(row,index)
		{
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open().setData(row)
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
	}
}
</script>
<style scoped>
.custom-tree-node {
	/*display: flex;*/
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	margin-left: 10px;
	display: none;
}

.custom-tree-node:hover .code {
	display: none;
}

.custom-tree-node:hover .do {
	margin-left: 10px;
	display: inline-block;
}
</style>
